package com.adds.lvds.security.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.security.model.SysRole;
import com.adds.lvds.security.service.SysRoleService;

@Controller
@RequestMapping("/security/role/")
public class SysRoleController extends BaseController<SysRole> {
	@Inject
	private SysRoleService roleService;

	@RequestMapping("list")
	public String list(Model model) {
		return "/security/role/listSysRole";
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @param resources
	 *            获取查询条件
	 * @param page
	 *            获取当前页数,page是jqGrid自带参数
	 * @param rows
	 *            获取每页显示行数,rows是jqGrid自带参数
	 * @param sidx
	 *            获取排序列名,sidx是jqGrid自带参数
	 * @param sord
	 *            获取排序方式,sord是jqGrid自带参数
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(HttpServletRequest request,
			HttpServletResponse response,
			@Param(value = "sysRole") SysRole sysRole,
			@Param(value = "page") String page,
			@Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx,
			@Param(value = "sord") String sord) {
		pageView = roleService.query(getPageView(page, rows, sidx, sord),
				sysRole);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageView.getRecords());
		map.put("records", pageView.getRowCount());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}

	/**
	 * 保存角色数据
	 * 
	 * @param model
	 * @param videoType
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addRole")
	@ResponseBody
	public Map<String, Object> addRole(@Param(value = "sysRole") SysRole sysRole) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = roleService.addSysRole(sysRole);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "录入角色数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "录入角色数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 加载编辑角色
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editRole")
	public String editRole(Model model, @Param(value = "roleId") String roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", roleId);
		SysRole role = roleService.getSysRoleById(map);
		model.addAttribute("role", role);
		return "/security/role/editSysRole";
	}

	/**
	 * 执行编辑角色
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "doEditRole")
	public Map<String, Object> doEditRole(
			@Param(value = "sysRole") SysRole sysRole) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = roleService.updateSysRole(sysRole);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "编辑角色数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "编辑角色数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "delRole")
	public Map<String, Object> delRole(Model model, @Param(value = "ids")String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = roleService.deleteSysRoleById(ids);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "删除角色数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "删除角色数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 通过accountId获取该id的绑定用户信息
	 * 
	 * @param model
	 * @param accountId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "findbyAccountRole")
	public List<SysRole> findbyAccountRole(Model model, String accountId) {
		return roleService.findbyUserRole(accountId);
	}

	@ResponseBody
	@RequestMapping(value = "addAccRole")
	public Map<String, Object> addAccRole(Model model, String accountId,
			String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (!StringUtils.isEmpty(ids)) {
				String[] roleIds = ids.split(",");
				roleService.addUserRole(accountId, Arrays.asList(roleIds));
			}
			map.put("flag", "true");
		} catch (Exception e) {
			map.put("flag", "false");
		}
		return map;
	}
}